import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { ElNotification } from "element-plus"; //处理导航信息
import { LineCruiseGroup, LineCruiseItems } from "@/api/backend/index";
import { mapCompanies, cascaderTree } from "@/utils/common";

export const useStandard = defineStore(Names.Standard, {
  state: () => {
    return {
      groupId: 0, //标准项相应的标准id
      groupIds: [],
      item: {}, //单个标准
      opera: "add",
      tag: {},
      pageSize: 10,
      pageNum: 1,
      options: <any>[],
      equipmentInfos: <any>[], //传递数组对象 设备包含标准id复杂数据
      itemsIndex: 0, //巡检设备索引
    };
  },
  getters: {},
  actions: {
    //获取巡检标准列表数据
    getStandardList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(LineCruiseGroup + "/list", filter, "json", "").then((res: any) => {
          console.log("获取巡检标准列表数据", res);
          this.options = cascaderTree(res.rows, "aim", "id");
          resolve(res);
        });
      });
    },
    //新增标准项
    addStandardSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _post(LineCruiseItems, data, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //修改标准项
    editStandardSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put(LineCruiseItems, data, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //查询标准项
    getStandardSet(type: string) {
      return new Promise((resolve, reject) => {
        _get(LineCruiseItems + `/${type}`, "", "json", "").then((res: any) => {
          console.log("查询标准项", res);
          resolve(res.data);
        });
      });
    },
    //删除标准项
    deleteStandardSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(LineCruiseItems + `/${data.id}`, {}, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    // 删除标准
    deleteStandard(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(LineCruiseGroup + `/${data.id}`, {}, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
